import { fileURLToPath, URL } from 'node:url'
import { defineConfig, mergeConfig, type UserConfig } from 'vite'

import vue from '@vitejs/plugin-vue'
import vueJsx from '@vitejs/plugin-vue-jsx'

import viteConfigDev from './vite.config.dev'
import viteConfigBuild from './vite.config.build'

export default defineConfig(({ command }) => {
  if (command === 'serve') {
    return mergeConfig(viteConfigBase(), viteConfigDev())
  }
  return mergeConfig(viteConfigBase(), viteConfigBuild())
})

const viteConfigBase = (): UserConfig => {
  return {
    // base: '/pad',
    plugins: [vue(), vueJsx()],
    resolve: {
      alias: {
        '@': fileURLToPath(new URL('../src', import.meta.url))
      }
    },
    define: {
      __DEV__: process.env.NODE_ENV !== 'production',
      __DESKTOP__: !!process.env.__DESKTOP__
    },
    envPrefix: ['VITE_', 'TAURI_']
  }
}
